﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;

namespace BaseData.WebApi.Controllers
{
    [ApiController]
    public class MaterialController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMaterialQueries _materialQueries;

        public MaterialController(IMediator mediator,
            IMaterialQueries materialQueries)
        {
            _mediator = mediator;
            _materialQueries = materialQueries;
        }

        [HttpGet(ApiRoutes.Material.QueryMaterials)]
        public async Task<IActionResult> Get([FromQuery] MaterialSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var result = await _materialQueries.QueryMaterialsByPage(model);
                return Ok(result);
            }
            else
            {
                var result = await _materialQueries.QueryMaterials(model);
                return Ok(result);
            }
        }

        [HttpPost(ApiRoutes.Material.CreateMaterial)]
        public async Task<IActionResult> Post(CreateMaterialCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.Material.GetMaterial)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _materialQueries.GetMaterial(id);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.Material.UpdateMaterial)]
        public async Task<IActionResult> Put(int id, UpdateMaterialCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }

    }
}